package com.app.newcsc.fragment.my.problem;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;

import com.app.newcsc.data.DemoRepository;
import com.app.newcsc.entity.Article;
import com.app.newcsc.entity.ArticleEntity;
import com.app.newcsc.entity.UpdateCongigEntity;
import com.app.newcsc.http.response.ApiException;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class ProblemDetailViewModel extends BaseViewModel<DemoRepository> {

    public SingleLiveEvent<UpdateCongigEntity> mUpdateCongigEntity = new SingleLiveEvent<>();
    public SingleLiveEvent<Article> mArticle = new SingleLiveEvent<>();//监听买卖按钮
    public ProblemDetailViewModel(@NonNull Application application, DemoRepository repository) {
        super(application, repository);
    }

    public void zdeskArtivle(String id) {
        //可以调用addSubscribe()添加Disposable，请求与View周期同步
        model.zdeskArtivle(id)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResponse<Article>>() {
                    @Override
                    public void onNext(BaseResponse<Article> response) {

                        Log.e("TAG","=====zdeskArtivle===onNext=============");
//                        dismissDialog();

                        if(response != null && response.getData() != null)
                        {
                            mArticle.postValue(response.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG","====zdeskArtivle====onError=============");
                        //关闭对话框
                        dismissDialog();
                        if (throwable instanceof ResponseThrowable) {
                            String msg = ApiException.getShowmessage(((ResponseThrowable) throwable).errorBody);
                            ToastUtils.showShort(msg);
                        }
                    }

                    @Override
                    public void onComplete() {
                        Log.e("TAG","=====zdeskArtivle===onComplete=============");
                        //关闭对话框
                        dismissDialog();
                    }
                });
    }

    public void requestNetWork(String id) {
        //可以调用addSubscribe()添加Disposable，请求与View周期同步
        model.getArticle(id)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResponse<List<ArticleEntity>>>() {
                    @Override
                    public void onNext(BaseResponse<List<ArticleEntity>> response) {

                        Log.e("TAG","========onNext=============");
//                        dismissDialog();
                        //请求成功
                        if (response.getCode() == 200) {
//                            mUpdateCongigEntity.postValue(response.getData());
                        } else {
                            //code错误时也可以定义Observable回调到View层去处理
                            ToastUtils.showShort("数据错误");
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG","========onError=============");
                        //关闭对话框
                        dismissDialog();
                        if (throwable instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) throwable).message);
                        }
                    }

                    @Override
                    public void onComplete() {
                        Log.e("TAG","========onComplete=============");
                        //关闭对话框
                        dismissDialog();
                    }
                });
    }

    public void apiConfig2() {
        //可以调用addSubscribe()添加Disposable，请求与View周期同步
        model.apiConfig2()
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResponse<UpdateCongigEntity>>() {
                    @Override
                    public void onNext(BaseResponse<UpdateCongigEntity> response) {

//                        dismissDialog();
                        //请求成功
                        if (response.getCode() == 200) {
                            mUpdateCongigEntity.postValue(response.getData());
                        } else {
                            //code错误时也可以定义Observable回调到View层去处理
                            ToastUtils.showShort("数据错误");
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        //关闭对话框
                        dismissDialog();
                        if (throwable instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) throwable).message);
                        }
                    }

                    @Override
                    public void onComplete() {
                        //关闭对话框
                        dismissDialog();
                    }
                });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}